package com.checcdata.cordova.activity;

import android.content.Context;
import android.view.View;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.AmapPageType;
import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.model.AMapNaviLocation;
import com.checcdata.cordova.entity.Position;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class AMapNavigationActivity implements INaviInfoCallback {
    public AMapNavigationActivity() {
    }

    public AMapNavigationActivity(Context context, JSONArray args) throws JSONException {
        try {
            Gson gson = new Gson();
            Position startPosition = gson.fromJson(args.getString(0), Position.class);
            LatLng startPoint = new LatLng(startPosition.getLat(), startPosition.getLng());
            Poi startPoi = new Poi(startPosition.getPoi(), startPoint, "");//起点

            JSONArray wayPositions = args.getJSONArray(1);
            List<Poi> wayPois = new ArrayList<Poi>(3);//目前支持三个途径点
            for (int i = 0; i < wayPositions.length() && i < 3; i++) {
                Position position = gson.fromJson(wayPositions.getString(i), Position.class);
                LatLng point = new LatLng(position.getLat(), position.getLng());
                Poi poi = new Poi(position.getPoi(), point, "");//途经点
                wayPois.add(poi);
            }

            Position endPosition = gson.fromJson(args.getString(2), Position.class);
            LatLng endPoint = new LatLng(endPosition.getLat(), endPosition.getLng());
            Poi endPoi = new Poi(endPosition.getPoi(), endPoint, "");//终点

            int aMapNaviTypeInt = args.getInt(3);
            AmapNaviType aMapNaviType = AmapNaviType.values()[aMapNaviTypeInt];

            int aMapPageTypeInt = args.getInt(4);
            AmapPageType aMapPageType = AmapPageType.values()[aMapPageTypeInt];

            AmapNaviParams amapNaviParams = new AmapNaviParams(startPoi, wayPois, endPoi, aMapNaviType, aMapPageType);
            amapNaviParams.setUseInnerVoice(true);
            AmapNaviPage.getInstance().showRouteActivity(context, amapNaviParams, this);
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    @Override
    public void onInitNaviFailure() {

    }

    @Override
    public void onGetNavigationText(String s) {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

    }

    @Override
    public void onArriveDestination(boolean b) {

    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onCalculateRouteSuccess(int[] ints) {

    }

    @Override
    public void onCalculateRouteFailure(int i) {

    }

    @Override
    public void onStopSpeaking() {

    }

    @Override
    public void onReCalculateRoute(int i) {

    }

    @Override
    public void onExitPage(int i) {

    }

    @Override
    public void onStrategyChanged(int i) {

    }

    @Override
    public View getCustomNaviBottomView() {
        return null;
    }

    @Override
    public View getCustomNaviView() {
        return null;
    }

    @Override
    public void onArrivedWayPoint(int i) {

    }
}
